﻿#include "ParallelBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

ParallelBlock::ParallelBlock(QString &name, QRect &position, double zoom, bool isExpand, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    m_isExpand = isExpand;
}

ParallelBlock::~ParallelBlock()
{

}

void ParallelBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = static_cast<double>(m_aScale) * zoom();
    double b = static_cast<double>(m_bScale)  * a;
    double bottom = y + height;

    SetNewPen(2)
    double iZ = zoom();
    double arrowH = 12*iZ;
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + arrowH));
    drawBottomArrow(canvas, QPointF(hcenter, y + arrowH), QSize(static_cast<int>(6 * zoom()), static_cast<int>(arrowH)));

    QPointF par[3][4];
    for(int i = 0; i < 3; i ++)
    {
        par[i][0] = QPointF(hcenter - b/2 + 3*i, y + arrowH + a/2 + 3*i);
        par[i][1] = QPointF(hcenter + 3*i, y + arrowH + 3*i);
        par[i][2] = QPointF(hcenter + b/2 + 3*i, y + arrowH + a/2 + 3*i);
        par[i][3] = QPointF(hcenter + 3*i, y + arrowH + a + 3*i);
    }


    QColor bgColor = m_backgroundColor;
    if(!m_isExpand)
    {
        bgColor = m_collapseBgColor;
    }
    QBrush brush = canvas->brush();
    canvas->setBrush(bgColor);
    if(m_isExpand)
    {
        canvas->drawPolygon(par[0], 4);
    }
    else{
        for(int di = 2; di >= 0; di--)
        {
            canvas->drawPolygon(par[di], 4);
        }
        canvas->drawLine(QLineF(hcenter, y + arrowH + a, hcenter, y + height));
    }
    canvas->setBrush(brush);

    UseOldPen
    double rfWidth =  b - a/2 - 40;
    double rfHeight = a - 8 - a/4;
    QRectF textRect(hcenter - b/2 + a/4 + 20, y + arrowH+ 4 + a/8, rfWidth, rfHeight);
    canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(name()));

    int si = getBranchItem().size();
    if(si < 1)
        return;

    if(m_isExpand)
    {
        SetNewPen(2)
        QPen enablePen = canvas->pen();
        QPen disablePen(Qt::gray);
        disablePen.setWidthF(2);
        int half = si/2;
        if(si % 2 == 0)
        {
            for(int i = 0;i < si;i++)
            {
                QBlock *bl = getBranchItem().at(i);
                QPointF line[3];
                int ww = 0;
                if(i < half)
                {
                    ww = -1 * static_cast<int>(b)/2;
                }
                else
                {
                    ww = static_cast<int>(b)/2;
                }
                line[0] = QPointF(hcenter + ww, y + arrowH + a/2 );
                line[1] = QPointF(bl->m_dX + bl->m_dWidth/2, y + arrowH + a/2);
                line[2] = QPointF(bl->m_dX + bl->m_dWidth/2, bottom - 2*zoom());

                if (!bl->branchEnable())
                {
                    canvas->setPen(disablePen);
                }
                else
                {
                    canvas->setPen(enablePen);
                }
                canvas->drawPolyline(line, 3);
            }
        }
        else
        {
            for(int i = 0;i < si;i++)
            {
                QBlock *bl = getBranchItem().at(i);
                QPointF line[4];
                int ww = 0;
                int lineNum = 3;
                if( i == half)
                {
                    ww = 0;
                    lineNum = 4;
                    line[0] = QPointF(hcenter + ww, y + arrowH + a);
                    line[1] = QPointF(hcenter + ww, y + arrowH + a + a/3);
                    line[2] = QPointF(bl->m_dX+ bl->m_dWidth/2, y + arrowH +a + a/3);
                    line[3] = QPointF(bl->m_dX + bl->m_dWidth/2, bottom - 2*zoom());
                }
                else
                {
                    if(i < half)
                    {
                        ww = -1 * static_cast<int>(b)/2;
                    }
                    else if(i > half)
                    {
                        ww = static_cast<int>(b)/2;
                    }
                    line[0] = QPointF(hcenter + ww, y + arrowH + a/2);
                    line[1] = QPointF(bl->m_dX + bl->m_dWidth/2, y + arrowH + a/2);
                    line[2] = QPointF(bl->m_dX + bl->m_dWidth/2, bottom - 2*zoom());
                }
                if (!bl->branchEnable())
                {
                    canvas->setPen(disablePen);
                }
                else
                {
                    canvas->setPen(enablePen);
                }
                canvas->drawPolyline(line, lineNum);
            }
        }

        for(int i = 0;i < si;i++)
        {
            QBlock *bl = getBranchItem().at(i);
            QPointF pRect[4];
            pRect[0] = QPointF(bl->m_dX + bl->m_dWidth/2 - rfWidth/4, y + arrowH + a + 1*zoom());
            pRect[1] = QPointF(bl->m_dX + bl->m_dWidth/2 + rfWidth/4, y + arrowH + a + 1*zoom());
            pRect[2] = QPointF(bl->m_dX + bl->m_dWidth/2 + rfWidth/4, y + arrowH + a + 13*zoom());
            pRect[3] = QPointF(bl->m_dX + bl->m_dWidth/2 - rfWidth/4, y + arrowH + a + 13*zoom());

            QBrush brush = canvas->brush();
            canvas->setBrush(QColor("#FFFFD7"));
            canvas->drawPolygon(pRect, 4);
            canvas->setBrush(brush);

            UseOldPen
            QRectF textRect(bl->m_dX + bl->m_dWidth/2 - rfWidth/4, y + arrowH + a + 1*zoom(), rfWidth/2, 12*zoom());
            canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(i+1));
        }

        canvas->setPen(enablePen);
        QBlock *left = getBranchItem().at(0);
        QBlock *right = getBranchItem().at(si-1);
        // connect
        QPointF leftEnd = QPointF(left->m_dX + left->m_dWidth / 2, bottom - 2*zoom());
        QPointF rightEnd = QPointF(right->m_dX + right->m_dWidth / 2, bottom - 2*zoom());
        canvas->drawLine(leftEnd,rightEnd);
        canvas->drawLine(static_cast<int>(hcenter),static_cast<int>(bottom) - 2*static_cast<int>(zoom()),static_cast<int>(hcenter), static_cast<int>(bottom) - 2*static_cast<int>(zoom()) + static_cast<int>(a)/4);

        UseOldPen
    }
    else{
        UseOldPen
        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter + b/3, y + 12 * zoom() + a/3), QObject::tr("Fold"));
    }
}

QString ParallelBlock::getBlockDom(int num)
{
    QString branchStr;
    for(int i = 0;i < num;i++)
    {
        branchStr += "<branch />";
    }
    return  QString("<algorithm><branch><parallel cond=\"branch=%1\" id=\"%2\" category=\"%3\" code=\"Parallel\" isPlugin=\"false\">"+branchStr+"</parallel></branch></algorithm>");
}

bool ParallelBlock::getBranchNum(QPointF &p, int &branchId)
{
    int si = getBranchItem().size();
    if(si < 1 || !m_isExpand)
        return false;
    double y=postion().y();
    double a = static_cast<double>(m_aScale) * zoom();
    double b = static_cast<double>(m_bScale)  * a;
    double rfWidth =  b - a/2 - 40;
    double iZ = zoom();
    double arrowH = 12*iZ;

    double curX = p.x();
    double curY = p.y();
    for(int i = 0;i < si;i++)
    {
        QBlock *bl = getBranchItem().at(i);
        double x1 = bl->m_dX + bl->m_dWidth/2 - rfWidth/4;
        double x2 = bl->m_dX + bl->m_dWidth/2 + rfWidth/4;
        double y1 = y + arrowH + a + 1*zoom();
        double y2 = y + arrowH + a + 13*zoom();
        if((curX >= x1 && curX <= x2) && (curY >= y1 && curY <= y2))
        {
            branchId = i+1;
            return  true;
        }
    }

    return false;
}
}
}
